<?php

    namespace App\Http\Controllers\Admin;

    use App\Models\Admin\Region;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;

    class IndexController extends Controller {
        //首页
        public function index() {
            $title = 'iseeAdmin 【首页】';
            return view('admin.index.index', [
                'title' => $title
            ]);
        }

        /**
         * 地区列表
         */
        public function list() {
            $title = '全国省市县';
            return view('admin.index.list', [
                'title' => $title
            ]);
        }

        /**
         * 获取列表
         * @param Request $request
         */
        public function getLists(Request $request) {
            try {
                $return['errCode'] = 1;
                $return['errMsg'] = 'ok';
                $return['result'] = [];

                $model = new Region();
                $searchVal = $request->input('searchVal');
//                dump($searchVal);
                $level = $request->input('level');

                $limit = intval($request->input('limit'));
                $currentPage = intval($request->input('page'));
                $offset = ($currentPage - 1) * $limit;

                if (!empty($searchVal)) {
                    switch ($level) {
                        case '-1':
                            $data = $model::where('name', 'like', '%' . $searchVal . '%')->offset($offset)->limit($limit)->get();
                            $total = $model::where('name', 'like', '%' . $searchVal . '%')->count();//总记录数
                            break;
                        default:
                            $data = $model::where('name', 'like', '%' . $searchVal . '%')->where(['level'=>$level])->offset($offset)->limit($limit)->get();
                            $total = $model::where('name', 'like', '%' . $searchVal . '%')->where(['level'=>$level])->count();//总记录数
                            break;
                    }
                } else {
                    $data = $model::offset($offset)->limit($limit)->get();
                    $total = $model->count();//总记录数
                }

                $result['total'] = $total;
                $result['currentPage'] = $currentPage;
                $result['limit'] = $limit;
                $result['data'] = $data;
                $return['result'] = $result;
            } catch (\Exception $e) {
                $return['errCode'] = -1;
                $return['errMsg'] = $e->getMessage();
            }
            return json_encode($return);
        }

        /**
         * 更新数据
         * @param Request $request
         */
        public function update(Request $request) {
            try {
                $return['errCode'] = 1;
                $return['errMsg'] = 'ok';
                $return['result'] = [];

                $id = intval($request->input('id'));
                $index = intval($request->input('index'));
                $val = $request->input('val');
                $model = Region::find($id);
                switch ($index) {
                    case 1:
                        $model->name = $val;
                        break;
                    case 2:
                        $model->letter = $val;
                        break;
                    case 3:
                        $model->zip_code = $val;
                        break;

                }
                $model->save();
            } catch (\Exception $e) {
                $return['errCode'] = -1;
                $return['errMsg'] = $e->getMessage();
            }
            return json_encode($return);
        }


    }
